package com.laowang.project.pay.config;



import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WxMsgPush {

  @Autowired
  private WeixinOpen weixinOpen;



  /**
   * 微信公众号API的Service
   */
  //@Autowired
  private final WxMpService wxMpService;

  /**
   * 构造注入
   */
  public WxMsgPush(WxMpService wxMpService) {
    this.wxMpService = wxMpService;
  }

  /**
   * 发送微信模板信息  模板1
   *
   * @param openId 接受者openId
   * @return 是否推送成功
   */
  public Boolean SendWxMsg(String openId, ReceiveOrderVo receiveOrderVo) {
    // 发送模板消息接口
    WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
        // 接收者openid
        .toUser(openId)
        // 模板id
        .templateId(weixinOpen.getTemplateId())
        // 模板跳转链接   h5 的地址
        //.url("pages/admin-order-list/order-list")
        .miniProgram(new WxMpTemplateMessage.MiniProgram(WxConfig.APP_ID,"pages/index/index",false))
        .build();
    // 添加模板数据
    /*if (receiveOrderVo.getUrl() != null){
      templateMessage.setUrl(receiveOrderVo.getUrl());
    }*/
    templateMessage.addData(new WxMpTemplateData("thing3", receiveOrderVo.getThing3(), "#FF00FF")) //商品名称
        .addData(new WxMpTemplateData("time4", receiveOrderVo.getTime4(), "#A9A9A9")) //下单时间
        .addData(new WxMpTemplateData("amount5",receiveOrderVo.getAmount5(), "#FF00FF"))//支付金额
        .addData(new WxMpTemplateData("number9",receiveOrderVo.getNumber9(),"#000000"));//会员号码
    String msgId = null;
    try {
      // 发送模板消息
      msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
    } catch (WxErrorException e) {
      e.printStackTrace();
    }
    log.warn("·==++--·推送微信模板信息：{}·--++==·", msgId != null ? "成功" : "失败");
    return msgId != null;
  }


  /**
   * 发送微信模板信息  模板1
   *
   * @param openId 接受者openId
   * @return 是否推送成功
   */
  public Boolean SendWxMsg1(String openId, ReceiveOrderVo1 receiveOrderVo1) {
    // 发送模板消息接口
    WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
        // 接收者openid
        .toUser(openId)
        // 模板id
        .templateId(weixinOpen.getTemplateId1())
        // 模板跳转链接   h5 的地址
        //.url("https://www.zhugongjia.cn/header/index.html")
        .build();
    // 添加模板数据
              /*  {{first.DATA}}
                申请人：{{keyword1.DATA}}
                申请时间：{{keyword2.DATA}}
                申请状态：{{keyword3.DATA}}
                {{remark.DATA}}*/
    templateMessage.addData(new WxMpTemplateData("amount1", receiveOrderVo1.getAmount1(), "#FF00FF"))
        .addData(new WxMpTemplateData("time3", receiveOrderVo1.getTime3(), "#A9A9A9"))
        .addData(new WxMpTemplateData("thing11",receiveOrderVo1.getThing11(), "#FF00FF"));

    String msgId = null;
    try {
      // 发送模板消息
      msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
    } catch (WxErrorException e) {
      e.printStackTrace();
    }
    log.warn("·==++--·推送微信模板信息：{}·--++==·", msgId != null ? "成功" : "失败");
    return msgId != null;
  }


  public Boolean SendWxMsg2(String openId, ReceiveOrderVo2 receiveOrderVo2) {
    // 发送模板消息接口
    WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
        // 接收者openid
        .toUser(openId)
        // 模板id
        .templateId(weixinOpen.getTemplateId2())
        // 模板跳转链接   h5 的地址
        //.url("https://www.zhugongjia.cn/header/index.html")
        .build();
    // 添加模板数据
              /*  {{first.DATA}}
                申请人：{{keyword1.DATA}}
                申请时间：{{keyword2.DATA}}
                申请状态：{{keyword3.DATA}}
                {{remark.DATA}}*/
    templateMessage.addData(new WxMpTemplateData("character_string1", receiveOrderVo2.getCharacter_string1(), "#FF00FF"))
        .addData(new WxMpTemplateData("thing2", receiveOrderVo2.getThing2(), "#A9A9A9"))
        .addData(new WxMpTemplateData("time5",receiveOrderVo2.getTime5(), "#FF00FF"));

    String msgId = null;
    try {
      // 发送模板消息
      msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
    } catch (WxErrorException e) {
      e.printStackTrace();
    }
    log.warn("·==++--·推送微信模板信息：{}·--++==·", msgId != null ? "成功" : "失败");
    return msgId != null;
  }
}
